package com.carl.service.impl;

import com.carl.common.ApiResponse;
import com.carl.common.ReturnMapUtil;
import com.carl.model.DataItem;
import com.carl.service.ApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author YuanRL
 * @version 1.0
 * @date 2025/3/17 16:00
 */
@Slf4j
@Service
public class ApiServiceImpl implements ApiService {

    @Override
    public ResponseEntity<ApiResponse> uploadFile(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook wb = new XSSFWorkbook(inputStream);
            XSSFSheet sheetAt = wb.getSheetAt(0);
            XSSFRow row = sheetAt.getRow(1);
            log.info(row.getCell(1).getStringCellValue());
            log.info(row.getCell(2).getStringCellValue());
            log.info(row.getCell(3).getStringCellValue());
            log.info(row.getCell(4).getStringCellValue());
            log.info(row.getCell(5).getStringCellValue());
            return ResponseEntity.ok(new ApiResponse(true, "文件上传成功"));
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.ok(new ApiResponse(false, "文件上传失败：" + e.getMessage()));
        }
    }

    @Override
    public List<DataItem> getDataList() {
        DataItem dataItem = new DataItem();
        dataItem.setId(1L);
        dataItem.setDate(new Date());
        dataItem.setTodayWordCount(18);
        dataItem.setIncrement(12);
        dataItem.setIncrement(12);
        return Collections.singletonList(dataItem);
    }
}
